package com.atguigu.service.impl;

import com.atguigu.dao.ServerTypeDao;
import com.atguigu.dao.impl.ServerTypeDaoImpl;
import com.atguigu.pojo.Page;
import com.atguigu.pojo.ServerType;
import com.atguigu.pojo.ServerType;
import com.atguigu.service.ServerTypeService;
import com.atguigu.utils.WebUtils;

import java.util.List;

public class ServerTypeServiceImpl implements ServerTypeService {
    ServerTypeDao serverTypeDao=new ServerTypeDaoImpl();//这里面是sql,接受参数,组织sql,执行mysql查询;所以它是公共的的类,增删改查就用它的方法实现
    @Override
    public void addServerType(ServerType serverType) {
        serverTypeDao.addServerType(serverType);
    }

    @Override
    public void updateServerType(ServerType serverType) {
        serverTypeDao.updateServerType(serverType);
    }

    @Override
    public void deleteServerTypeById(String id) {
        serverTypeDao.deleteServerTypeById(id);
    }

    @Override
    public ServerType queryServerTypeById(String id) {
        ServerType serverType=serverTypeDao.queryServerTypeById(id);
        return serverType;
    }

    @Override
    public String queryServerTypeIdByName(String name){
        ServerType serverType=serverTypeDao.queryServerTypeByName(name);
        return serverType.getId();
    }

    @Override
    public ServerType queryServerTypeByName(String name){
        ServerType serverType=serverTypeDao.queryServerTypeByName(name);
        return serverType;
    }
    @Override
    public List<ServerType> queryServerTypes() {
        List<ServerType> queryServerTypes=serverTypeDao.queryServerTypes();
        return queryServerTypes;
    }

    @Override
    public Page page(Integer pageNo, Integer pageSize) {
        Page<ServerType> page=new Page<>();
        page.setPagesize(pageSize);
        Integer pageTotalCount=serverTypeDao.queryForPageTotalCount();
        page.setPageTotalCount(pageTotalCount);
        Integer pageTotal=pageTotalCount/pageSize;
        if(pageTotalCount % pageSize >0){
            pageTotal++;//总条数,除以一页条数,大于0,页码数+1
        }
        page.setPageTotal(pageTotal);
        System.out.println("pageNo 原来是 is:"+pageNo);
        if(pageNo<1){//防止用户输入量太大
            pageNo=1;
        }else if(pageNo>pageTotal){
            pageNo=pageTotal;
        }
        page.setPageNo(pageNo);//当前页码
        System.out.println("pageNo 现在是 is:"+pageNo);
        System.out.println("pageTotal is:"+pageTotal);
        //要得到的是page的items
        Integer begin=(page.getPageNo()-1)*pageSize;
        List<ServerType> items=serverTypeDao.queryForPageItems(begin,pageSize);
        page.setItems(items);
        return page;
    }
}
